package main

import (
	"fmt"
	"time"
)

//取得当前月份的起始日期
func main() {
	/*
		var (
			y int
			m time.Month
		)

		y, m, _ = time.Now().Date()
		first, last := monthInterval(y, m)
		fmt.Println(first.Format("2006-01-02"))
		fmt.Println(last.Format("2006-01-02"))
	*/

	t := time.Now()
	//fmt.Println(BeginningOfMonth(t))
	//fmt.Println(EndOfMonth(t))

	// If you need only date use Format(). But remember Format() will return as a string
	dateFormat := "2006-01-02"
	//dateFormat := "20060102"
	//fileSuffix := t.Format(dateFormat)
	//fileName := fmt.Sprintf("%s-%s.csv", "CME终端数据", fileSuffix)
	//fmt.Println(fileName)
	//fmt.Println(t.Format(dateFormat))
	fmt.Println(BeginningOfMonth(t).Format(dateFormat)) //2021-11-01
	fmt.Println(EndOfMonth(t).Format(dateFormat))       //2021-11-30

	//s := ""
	//splits := strings.Split(s, ",")
	//fmt.Println(len(splits))
	//for _, v := range splits {
	//	fmt.Println(v)
	//	fmt.Println(len(v))
	//}
	//fmt.Println(t.Format("2006-01-02 15:04:05"))

	/*
		address := "云南省/昆明市/五华区"
		fmt.Println(len(strings.Split(address, "/")))
		slice := strings.Split(address, "/")[:2]
		fmt.Println(strings.Join(slice, "/"))
		country := strings.Split(address, "/")[2]
		fmt.Println(country)
	*/
}

func monthInterval(y int, m time.Month) (firstDay, lastDay time.Time) {
	firstDay = time.Date(y, m, 1, 0, 0, 0, 0, time.UTC)
	lastDay = time.Date(y, m+1, 1, 0, 0, 0, -1, time.UTC)
	return firstDay, lastDay
}

func BeginningOfMonth(t time.Time) time.Time {
	return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
}

func EndOfMonth(t time.Time) time.Time {
	return BeginningOfMonth(t).AddDate(0, 1, 0).Add(-time.Second)
}
